package com.ecp.commons.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

public class SetWebContextFilter extends HttpServlet implements Filter {

	private static final long	serialVersionUID	= 1L;

	public SetWebContextFilter() {
	}

	public void init(FilterConfig filterConfig) throws ServletException {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		WebContext ctx = new WebContext(req, res);

		ctx.setColumnId(-1);
		String sColId = request.getParameter("columnID");
		if (StringUtils.isNumeric(sColId)) {
			int iColumnId = Integer.parseInt(sColId);
			ctx.setColumnId(iColumnId);
		}

		WebContextFactory.setWebContext(ctx);

		chain.doFilter(request, response);
	}
}
